﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shl01.Models
{
    public class BLl
    {
        public string TypeName { get; set; }//表名
        public string PrimaryName { get; set; }//主键名
        public string DBName { get; set; }//数据库名

        public string GetStr()
        {
            string str = "";
            str = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using " + DBName + @".DAL;
using " + DBName + @".Models;
using System.Data.SqlClient;

namespace " + DBName + @".BLL
{
    [Serializable]
    public static class GradeManager
    {

        /// <summary>
        /// 获取满足条件的数量
        /// </summary>
        /// <param name='id'></param>
        /// <param name='error'></param>
        /// <returns></returns>
        public static int GetCount(string id,ErrorVM error = null)
        {
            try
            {
                string where = ' and id = @id';
                SqlParameter[] paras =
                {
                    new SqlParameter('@id',id)
                };
                return GradeService.GetCount(where, paras);
               
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Exception = ex;
                    error.Message = ex.Message;
                }
            }

            return 0;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name='count'>总页数</param>
        /// <param name='ye'>页码</param>
        /// <param name='size'>页数量</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static List<" + DBName + ".Models." + @"Grade> GetAllFen(out int count,int ye = 1,int size=10, ErrorVM error = null)
        {
            try
            {
                return GradeService.GetByFenYe(out count,ye,size);
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Exception = ex;
                    error.Message = ex.Message;
                }
            }
            count = 0;
            return null;
        }

        /// <summary>
        /// 根据主键判断是否存在
        /// </summary>
        /// <param name='id'>主键</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static bool IsExitsHas(string id, ErrorVM error = null)
        {
            string where = ' and " + PrimaryName + @" = @id';

            SqlParameter[] paras =
            {
                new SqlParameter('@id',id)
            };
            try
            {
                return GradeService.GetAllList(where,paras).Count == 1;
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return false;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name='result'>要添加的数据</param>
        /// <param name='error'>错误信息</param>
        /// <returns>是否成功</returns>
        public static bool Add(" + DBName + ".Models." + @"Grade result, ErrorVM error = null)
        {
            //判断是否存在
            if (IsExitsHas(result.id.ToString()))
            {
                if (error != null)
                {
                    error.Message = '数据重复';
                    error.Exception = new Exception('数据重复');
                }
                return false;
            }
            try
            {
                return GradeService.Add(result) == 1;
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return false;
        }

        /// <summary>
        /// 根据主键获取内容
        /// </summary>
        /// <param name='id'>主键</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static " + DBName + ".Models." + @"Grade GetGradeById(string " + PrimaryName + @",ErrorVM error = null)
        {
            List<" + DBName + ".Models." + @"Grade> list = null;
            try
            {
                string where = ' and " + PrimaryName + @" = @" + PrimaryName + @"';
                SqlParameter[] paras =
                {
                    new SqlParameter('@" + PrimaryName + @"'," + PrimaryName + @")
                };
                list = GradeService.GetAllList(where, paras);
                if(list!=null && list.Count > 0)
                    return list[0];
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static List<" + DBName + ".Models." + @"Grade> getAllGrade(ErrorVM error = null)
        {
            List<" + DBName + ".Models." + @"Grade> list = null;
            try
            {
                list = GradeService.GetAllList();
                return list;
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return null;
        }

        /// <summary>
        /// 根据对象删除数据
        /// </summary>
        /// <param name='result'>对象</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static bool Delete(" + DBName + ".Models." + @"Grade result, ErrorVM error = null)
        {
            try
            {
                return GradeService.Delete(result." + PrimaryName + @") == 1;
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return false;
        }

        /// <summary>
        /// 根据id删除数据
        /// </summary>
        /// <param name='id'>id</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static bool Delete(int id,ErrorVM error = null)
        {
            try
            {
                return GradeService.Delete(id) == 1;
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return false;
        }

        /// <summary>
        /// 根据多个主键删除多个
        /// </summary>
        /// <param name='results'>主键集合</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static int Delete(string[] results,ErrorVM error = null)
        {
            StringBuilder sb = new StringBuilder('0');
            foreach (string id in results)
            {
                sb.Append(',' + id);
            }
            string where = ' and " + PrimaryName + @" in (' + sb + ')';
            try
            {
                return GradeService.Delete(where);
            }
            catch (Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return 0;
        }
        
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name='result'>对象</param>
        /// <param name='error'>错误信息</param>
        /// <returns></returns>
        public static bool Update(" + DBName + ".Models." + @"Grade result,ErrorVM error = null)
        {
            try
            {
                return GradeService.Update(result) == 1;
            }
            catch(Exception ex)
            {
                if (error != null)
                {
                    error.Message = ex.Message;
                    error.Exception = ex;
                }
            }

            return false;
        }
    }
}
";
            str = str.Replace("Grade",TypeName);
            return str.Replace("'", "\"");
        }
    }
}
